/**
 * 安全奖励申请 编辑页
 *
 * 作者 wanghjw
 * 日期 2024-09-03 14:08:36
 */
layui.use(['jquery','febs', 'form', 'validate','autocomplete','table','commonJS','util', 'fileUpload'], function () {
    var $ = layui.$,
        febs = layui.febs,
        layer = layui.layer,
        form = layui.form,
        table = layui.table,
        commonJS = layui.commonJS,
        fileUpload = layui.fileUpload,
        util = layui.util,
        autocomplete = layui.autocomplete,
        $view = $('#incentiveRewardAudit-edit'),
        validate = layui.validate,
        tableIns,
        tableIns1;



    form.verify(validate);
    form.render();
    var fileUploadDiv = fileUpload.render({
        elem: '#fileUpload',
        url: ctx + 'incentive/upload',
        type: 2,//0-图片，1-视频，2-其他
        maxNum:5,
        defaultData: incentiveRewardAudit && incentiveRewardAudit.fileList ? incentiveRewardAudit.fileList:[],
        isDetail:true,
        done: function (res) {
        },
    });

    initFormValue();




    function initFormValue() {
        if (incentiveRewardAudit) {
            form.val("incentiveRewardAudit-edit-form", {
                "rewardApplyId": incentiveRewardAudit.rewardApplyId,
                "tenantId": incentiveRewardAudit.tenantId,
            })
            if(incentiveRewardAudit.rewardAmountFlg == '1'){
                $view.find('input[name="rewardAmountFlg"]').attr('checked','true');
            }
            if(incentiveRewardAudit.rewardScoreFlg == '1'){
                $view.find('input[name="rewardScoreFlg"]').attr('checked','true');
            }
            if(incentiveRewardAudit.rewardGoodsFlg == '1'){
                $view.find('input[name="rewardGoodsFlg"]').attr('checked','true');
            }
            if(!commonJS.isEmpty(incentiveRewardAudit.userList)){
                // var userList = [];
                incentiveRewardAudit.userList.forEach((item, index) => {
                    item.uname=item.realName
                });
               initUserTable(incentiveRewardAudit.userList);
            }
            if(!commonJS.isEmpty(incentiveRewardAudit.goodsList)){
                initGoodsTable(incentiveRewardAudit.goodsList);
            }else {
                initGoodsTable([]);
            }
        }
    }



    form.on('submit(incentiveRewardAudit-edit-form-submit)', function (data) {
        addOrUpdate(data.field);
        return false;
    });

    function addOrUpdate(data) {
        var msg1 = '审核提示！';
        var msg2 = '是否审核？';
        if(data.status == '3'){
            msg1 = '退回提示！';
            msg2 = '是否退回？';
        }
        febs.modal.confirm(msg1,msg2,function (index){
            febs.postArray(ctx + 'incentiveRewardAudit/audit', data, function () {
                layer.closeAll();
                febs.alert.success('审核成功');
                $('#febs-incentiveRewardAudit').find('#query').trigger('click');
            });
        })
    }
    function getTenantId(){
        if(incentiveRewardAudit){
            return incentiveRewardAudit.tenantId
        }else {
            return POPUP_DATA.tenantId
        }
    }



    /**
     * 加载奖励人员表
     * @param data
     */
    function initUserTable(data) {
        console.log('ren'+JSON.stringify(data))
        // debugger
        tableIns = febs.table.init({
            elem: $view.find('table[id="rewardUserTable"]'),
            id: 'rewardUserTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            cols: [[
                {field: 'userId',title: '用户ID',hide:true},
                {field: 'userNo', title: '工号', minWidth: 250,align:'center'},
                {field: 'uname', title: '姓名', minWidth: 250,align:'center'},
                {field: 'deptId', title: '部门ID', width: 120,hide:true},
                {field: 'deptName', title: '部门名称', width: 250,align:'center'}
            ]],
            data:data
        });

        form.render()
    }



    /**
     * 加载奖励商品表
     * @param data
     */
    function initGoodsTable(data) {
        tableIns1 = febs.table.init({
            elem: $view.find('table[id="rewardGoodsTable"]'),
            id: 'rewardGoodsTable',
            page:false,
            skin: '',
            limit:Number.MAX_VALUE,
            cols: [[
                {field: 'goodsId',title: '商品ID',hide:true},
                {field: 'goodsName', title: '商品名称', width: 500,align:'center'},
                {field: 'goodsNumber', title: '奖励数量', width: 300,align:'center'}
            ]],
            data:data
        });

        form.render()
    }







});